<template>
  <!--
    	作者：luoyiming
    	时间：2020-04-26
    	描述：门店-编辑门店
    -->
  <div class="product-add">
    <!--form表单-->
    <div>
      <div class="common-form">电子围栏</div>

      <div v-if="remarkAreaDialog">
        <GetpointRemark @confirmRemark="confirmRemark" ref="GetpointRemark" :form="form"  @getMapdata="getMapdataFunc" :clientHeight="800" :showcity="false" :showbutton="false"></GetpointRemark>
      </div>
    </div>
    <!--提交-->
    <div class="common-button-wrapper">
      <el-button size="small" type="info" @click="cancelFunc">返回</el-button>
    </div>
  </div>
</template>

<script>
import GetpointRemark from '@/components/map/GetpointRemark.vue';
import Getpoint from '@/components/map/Getpoint.vue';
import StoreApi from '@/api/store.js';
import Upload from '@/components/file/Upload';
import {formatModel} from '@/utils/base.js'

export default {
  components: {
    GetpointRemark,
    /*腾讯地图拾取器*/
    Getpoint,
    Upload
  },
  data() {
    return {
      remarkAreaDialog: false,
      mapOpen: false,
      disabled:false,

      /*form表单数据*/
      form: {
        showType:1,
        store_id: null,
        store_look_id: null,
        store_name: '',
        linkman: '',
        phone: '',
        shop_hours: '',
        province_id: '',
        city_id: '',
        region_id: '',
        address: '',
        coordinate: '',
        summary: '',
        sort: '',
        is_check: '',
        status: '',
        logo_image_id: 0,
        check_store: 0,
        refuse_content: '',
        scope_coordinates: []

      },
      /*省市区*/
      areaList: [],
      /*查询信息*/
      list: [],
      /*是否上传图片*/
      isupload: false,
      loading: false,
      file_path: ''
    };
  },
  async created() {

    this.form.store_id = this.$route.query.store_id;
    await this.getData();
    this.mapOpen = true;
    // setTimeout(() => {
      this.remarkAreaDialog = !this.remarkAreaDialog
    // }, 800);
    console.log('调用2',this.form.scope_coordinates)

  },
  mounted(){




  },
  watch: {
    remarkAreaDialog(){
      console.log('监听打开')
      let self = this
      if(this.remarkAreaDialog==true){
        console.log('监听打开666',self.form.scope_coordinates)
        // console.log(this.$refs['GetpointRemark'],'drawMarkers')
        setTimeout(() => {

          self.$refs['GetpointRemark'].drawMarkers(self.form.scope_coordinates);
        }, 200);
        //
      }else{
        this.$router.push('/store/store/storelist');
      }
    }
  },
  methods: {
    remarkAreaDialogOpenAndClose() {
      this.remarkAreaDialog = !this.remarkAreaDialog

    },


    checkStoreClick() {
      let self = this;
      let form = self.form;
      self.loading = true;
      StoreApi.checkStoreClick(form, true)
        .then(data => {
          self.loading = false;
          self.$message({
            message: '审核成功',
            type: 'success'
          });
          self.$router.push('/store/store/index');
        }).catch(error => {
        self.loading = false;
      });
    },
    /*修改*/
    onSubmit() {
      let self = this;
      let form = self.form;
      self.$refs.form.validate((valid) => {
        if (valid) {
          self.loading = true;
          StoreApi.editShop(form, true)
            .then(data => {
              self.loading = false;
              self.$message({
                message: '恭喜你，操作成功',
                type: 'success'
              });
              self.$router.push('/store/store/index');
            }).catch(error => {
            self.loading = false;
          });
        }
      });
    },
    confirmRemark(coordinates) {
      let self = this;
      let form = self.form;
      form.scope_coordinates = coordinates;
      // console.log(self.form)
      self.$refs.form.validate((valid) => {
        if (valid) {
          self.loading = true;
          StoreApi.editShop(form, true)
            .then(data => {
              self.loading = false;
              self.$message({
                message: '恭喜你，门店修改成功',
                type: 'success'
              });
              // self.$router.push('/store/store/index');
            }).catch(error => {
            self.loading = false;
          });
        }
      });
      this.remarkAreaDialog = false
    },
    /*初始化城市id*/
    initCity() {
      this.form.city_id = ''
    },
    /*初始化区id*/
    initRegion() {
      this.form.region_id = ''
    },

    /*获取经纬度*/
    getMapdataFunc(e) {
      this.form.coordinate = e.data[0].toFixed(6) + ',' + e.data[1].toFixed(6);
    },

    /*获取参数*/
    async getData() {
      return new Promise(async (resolve, reject) => {
        let self = this;
        StoreApi.storeedit({
          store_id: self.form.store_id
        }, true)
          .then(async(res) => {
            self.form = formatModel(self.form, res.data.model);
            // self.file_path = res.data.model.logo.file_path;
            self.form.is_check = res.data.model.is_check.value;
            self.form.status = res.data.model.status.value;
            self.areaList = res.data.regionData;
            this.mapOpen = true;
            console.log('调用1','success')
            resolve('ok');
          })
          .catch(async(error) => {
            reject(error);
          });
      });
    },

    /*上传*/
    openUpload(e) {
      this.type = e;
      this.isupload = true;
    },

    /*获取图片*/
    returnImgsFunc(e) {
      if (e != null) {
        this.file_path = e[0].file_path;
        this.form.logo_image_id = e[0].file_id;
      }
      this.isupload = false;
    },

    /*选择的地址*/
    choseFunc(e) {
      this.form.coordinate = e.location.lat + ',' + e.location.lng;
      this.form.address = e.address;
    },

    /*取消*/
    cancelFunc() {
      this.$router.back(-1);
    }

  }

};
</script>

<style lang="scss" scoped>
.basic-setting-content {
}

.product-add {
  padding-bottom: 50px;
}

.img {
  margin-top: 10px;
}
</style>
<style lang="scss">
.dialog-footer-at-remark-map{
  width: 100%;
  display: flex;
  justify-content: flex-end;
  margin-top: 30px;
}
</style>
